package com.ruoyi.client.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.GoodsQueryPageForm;
import com.ruoyi.client.pojo.vo.GoodsDetailVo;
import com.ruoyi.client.pojo.vo.GoodsVo;
import com.ruoyi.client.pojo.vo.PageListVo;
import com.ruoyi.client.service.ClientGoodsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品接口
 * @Author：HuaQP
 * @Date：2025/10/13
 */
@Anonymous // 允许匿名访问
@Api(tags = "3-商品接口")
@RestController
@RequestMapping("/goods")
public class ClientGoodsController extends BaseController {

    @Resource
    ClientGoodsService clientGoodsService;

    // ========== 新增：店铺分类查询接口（必须放在/{id}接口之前） ==========
    @ApiOperation(value = "查询店铺分类列表", notes = "根据店铺ID返回该店铺下的商品分类名称列表")
    @GetMapping("/store-categories")
    public R<List<String>> getStoreCategories(
            @ApiParam(value = "店铺ID", required = true) Integer storeId) {
        // 调用Service查询该店铺下的分类名称列表（去重）
        List<String> categoryList = clientGoodsService.getCategoryNamesByStoreId(storeId);
        return R.ok(categoryList);
    }

    /**
     * 查询商品分页列表
     */
    @ApiOperation(value = "查询商品分页列表", notes = "根据参数查询返回商品分页列表")
    @GetMapping("/page")
    public R<PageListVo<GoodsVo>> page(@Valid GoodsQueryPageForm form) {
        startPage();
        List<GoodsVo> list = clientGoodsService.queryPage(form);
        PageInfo pageInfo = new PageInfo(list);
        PageListVo<GoodsVo> pageListVo = new PageListVo<>();
        pageListVo.setTotalCount(pageInfo.getTotal());
        pageListVo.setTotalPage(pageInfo.getPages());
        pageListVo.setList(list);
        return R.ok(pageListVo);
    }


    /**
     * 查询商品详情 ById
     */
    @ApiOperation(value = "查询商品详情", notes = "传参为商品id")
    @GetMapping("/{id}")
// 将@PathVariable的类型改为Long
    public R<GoodsDetailVo> goodsDetail(@PathVariable @ApiParam(value = "商品id", required = true) Long id) {
        GoodsDetailVo goodsDetailVo = clientGoodsService.selectVoById(id);
        return R.ok(goodsDetailVo);
    }



    /**
     * 查询盲盒商品池
     */
    @ApiOperation(value = "查询盲盒商品池", notes = "随机返回可用商品列表，供盲盒抽签使用")
    @GetMapping("/pool")
    public R<List<GoodsVo>> getBlindBoxGoodsPool() {
        List<GoodsVo> goodsPool = clientGoodsService.queryBlindBoxGoods();
        return R.ok(goodsPool);
    }

}